<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 给你一个整数数组 nums ，请你找出一个具有最大和的连续子数组（子数组最少包含一个元素），返回其最大和 -->
    <!-- 子数组 是数组中的一个连续部分 -->
    <script>
      function maxSubArray(nums) {
        let sum = 0
        let res = []
        for (let i = 0; i < nums.length; i++) {
          sum = 0
          for (let j = i; j < nums.length; j++) {
            sum += nums[j]
            res.push(sum)
          }
        }
        return Math.max(...res)
      }
      // maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4])
      console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]))
      console.log(maxSubArray([1]))
      console.log(maxSubArray([5, 4, -1, 7, 8]))
    </script>
  </body>
</html>
